package cn.wolfcode.wolf2world.elasitcsearch.service.impl;

import cn.wolfcode.wolf2world.domain.Destination;
import cn.wolfcode.wolf2world.domain.Strategy;
import cn.wolfcode.wolf2world.domain.Travel;
import cn.wolfcode.wolf2world.domain.UserInfo;
import cn.wolfcode.wolf2world.elasitcsearch.domain.DestinationEs;
import cn.wolfcode.wolf2world.elasitcsearch.domain.StrategyEs;
import cn.wolfcode.wolf2world.elasitcsearch.domain.TravelEs;
import cn.wolfcode.wolf2world.elasitcsearch.domain.UserInfoEs;
import cn.wolfcode.wolf2world.elasitcsearch.query.SearchQueryObject;
import cn.wolfcode.wolf2world.elasitcsearch.service.SearchHighLightService;
import cn.wolfcode.wolf2world.elasitcsearch.service.SearchService;
import cn.wolfcode.wolf2world.service.DestinationService;
import cn.wolfcode.wolf2world.service.StrategyService;
import cn.wolfcode.wolf2world.service.TravelService;
import cn.wolfcode.wolf2world.service.UserInfoService;
import cn.wolfcode.wolf2world.utils.ParamMap;
import cn.wolfcode.wolf2world.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @author 杨兴健
 * @Date 2020/8/20 19:38
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private DestinationService destinationService;
    @Autowired
    private StrategyService strategyService;
    @Autowired
    private TravelService travelService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SearchHighLightService searchHighLightService;

    @Override
    public Object query(SearchQueryObject qo) {
        try {
            qo.setKeyword(URLDecoder.decode(qo.getKeyword(),"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        switch (qo.getType()) {
            case SearchQueryObject.TYPE_DEST :
                return this.queryDest(qo);
            case SearchQueryObject.TYPE_STRATEGY :
                return this.queryStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL :
                return this.queryTravel(qo);
            case SearchQueryObject.TYPE_USER :
                return this.queryUser(qo);
            default:return this.queryAll(qo);
        }
    }

    //处理目的地
    private Object queryDest(SearchQueryObject qo) {
        Destination destination = destinationService.queryByName(qo.getKeyword());
        if (destination != null) {
            //攻略
            List<Strategy> strategies = strategyService.queryByDestName(qo.getKeyword());
            //游记
            List<Travel> travels = travelService.queryByDestName(qo.getKeyword());
            travels.forEach(travel -> {
                travel.setAuthor(userInfoService.get(travel.getUserId()));
            });
            //用户
            List<UserInfo> userInfos = userInfoService.queryByCity(qo.getKeyword());
            //total 总数
            SearchVo vo = new SearchVo();
            vo.setStrategys(strategies);
            vo.setTravels(travels);
            vo.setUsers(userInfos);
            vo.setTotal(strategies.size()+travels.size()+userInfos.size());
            return ParamMap.newInstance().put("result",vo).put("qo",qo).put("dest",destination);
        }
        return null;
    }
    //处理攻略
    private Object queryStrategy(SearchQueryObject qo) {
        //
        Page<Strategy> page = searchHighLightService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");
        return ParamMap.newInstance().put("page",page).put("qo",qo);
    }

    //处理游记
    private Object queryTravel(SearchQueryObject qo) {
        //
        Page<Travel> page = searchHighLightService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo, "title","summary");
        return ParamMap.newInstance().put("page",page).put("qo",qo);
    }

    //处理用户
    private Object queryUser(SearchQueryObject qo) {
        //
        Page<UserInfo> page = searchHighLightService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo, "info","city");
        return ParamMap.newInstance().put("page",page).put("qo",qo);
    }

    //处理全部
    private Object queryAll(SearchQueryObject qo) {
        Page<Strategy> strategy = searchHighLightService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");
        Page<Travel> travel = searchHighLightService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo, "title","summary");
        Page<UserInfo> userInfo = searchHighLightService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo, "info","city");
        Page<Destination> destination = searchHighLightService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo, "name","info");
        SearchVo vo = new SearchVo();
        vo.setTotal(strategy.getTotalPages()+travel.getTotalPages()+userInfo.getTotalPages()+destination.getTotalPages());
        vo.setStrategys(strategy.getContent());
        vo.setUsers(userInfo.getContent());
        vo.setTravels(travel.getContent());
        vo.setDests(destination.getContent());

        return ParamMap.newInstance().put("result",vo).put("qo",qo);
    }
}
